layui.use(['form','layer','table','laydate'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate,form = layui.form;
    //第一个实例
    table.render({
        elem: '#demo'
        ,id:'tabuser'
        ,height: "auto"
        ,method:"post"
        ,url: baseUrl+'change/getusers?roomName='+$("#roomName").val() //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: true //开启分页
        ,cols: [[ //表头
            {field: 'userid', title: '用户ID',align:'center',fixed: 'left',width:50,sort:true}
            ,{field: 'userName', title: '用户名称',align:'center',sort:true}
            ,{field: 'usercards', title: '用户手牌',align:'center',sort:true}
            ,{field: 'nextCard', title: '下一张牌',align:'center',event:"edit",sort:true,toolbar: '#barDemo'}
        ]]

    });

    var id;
    //监听工具条
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'edit') { //编辑
            id = data.userid;
            var index = $(".active").attr("data-id");
            $.ajax({
                type: "POST", //提交方式
                url: baseUrl+'change/getcards?roomName='+$("#roomName").val(),
                dataType: 'json',
                data: {
                    pageName: '10' //页码的参数名称，默认：page
                    ,limitName:'10'
                },
                success: function(data){
                    var html = setBrand(JSON.stringify(data.data[0].cards),index);
                    $("#cards").html(html);
                }
            })
        }
    });

//加载剩余的牌
    function setBrand(brands,index){
        var html = "";
        var indexs = brands.split(",");
        indexs[0] = ~~indexs[0];
        indexs[indexs.length-1] = indexs[indexs.length-1].replace("]","")
        for(var i=0;i<indexs.length;i++){
            if(index != indexs[i]){
                html += "<img id='"+indexs[i]+"' onclick=onkey('"+indexs[i]+"','"+id+"') src='"+baseUrl+"img/"+indexs[i]+".png' width='40px' height='50px' />"
            }
        }
        return html;
    }



    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');
            var gameType = $('#gameType');
            var retype = gameType.val();

            //执行重载
            table.reload('tabuser', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    phone: demoReload.val(),
                    recordType:retype
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


})

